using IrisGenerator.Common.Core;
using IrisGenerator.Common.Tools;
using IrisGenerator.Framework.RuleEngine;
using IrisGenerator.Framework.Events;
namespace IrisGenerator.Common.Modules
{


    public partial class Module
    {

        private System.Nullable<System.Guid> _modId;

        private System.Nullable<System.Guid> _tarId;

        private string _modName;

        private string _modDescription;

        private string _modAssembly;

        private string _modType;

        private Target _target;

        private ModuleInstanceCollection _moduleInstanceCollection;

        private ModuleParameterCollection _moduleParameterCollection;

        private ModuleParameterCollection _moduleParameterCollection2;

        private OptionCollection _optionCollection;

        public virtual System.Nullable<System.Guid> ModId
        {
            get
            {
                return _modId;
            }
            set
            {
                _modId = value;
                this.OnPropertyChanged("ModId");
            }
        }

        public virtual string ModName
        {
            get
            {
                return _modName;
            }
            set
            {
                _modName = value;
                this.OnPropertyChanged("ModName");
            }
        }

        public virtual string ModDescription
        {
            get
            {
                return _modDescription;
            }
            set
            {
                _modDescription = value;
                this.OnPropertyChanged("ModDescription");
            }
        }

        public virtual string ModAssembly
        {
            get
            {
                return _modAssembly;
            }
            set
            {
                _modAssembly = value;
                this.OnPropertyChanged("ModAssembly");
            }
        }

        public virtual string ModType
        {
            get
            {
                return _modType;
            }
            set
            {
                _modType = value;
                this.OnPropertyChanged("ModType");
            }
        }

        public virtual Target Target
        {
            get
            {
                if ((this._target == null))
                {
                    this._target = Target.Load(this._tarId);
                }
                return this._target;
            }
            set
            {
                _target = value;
                if (_target != null)
                    this._tarId = _target.TarId;
                else
                    this._tarId = null;
            }
        }

        public virtual ModuleInstanceCollection ModuleInstanceCollection
        {
            get
            {
                if ((this._moduleInstanceCollection == null))
                {
                    _moduleInstanceCollection = ModuleInstance.Select_MODULE_INSTANCE_By_MOD_ID(this.ModId);
                }
                return this._moduleInstanceCollection;
            }
        }

        public virtual ModuleParameterCollection ModuleParameterCollection
        {
            get
            {
                if ((this._moduleParameterCollection == null))
                {
                    _moduleParameterCollection = ModuleParameter.Select_MODULE_PARAMETER_By_MOD_ID(this.ModId);
                }
                return this._moduleParameterCollection;
            }
        }

        public virtual ModuleParameterCollection ModuleParameterCollection2
        {
            get
            {
                if ((this._moduleParameterCollection2 == null))
                {
                    _moduleParameterCollection2 = ModuleParameter.Select_MODULE_PARAMETER_By_MOD_ID(this.ModId);
                }
                return this._moduleParameterCollection2;
            }
        }

        public virtual OptionCollection OptionCollection
        {
            get
            {
                if ((this._optionCollection == null))
                {
                    _optionCollection = Option.Select_OPTION_By_MOD_ID(this.ModId);
                }
                return this._optionCollection;
            }
        }

        private void Clean()
        {
            this.ModId = null;
            this._tarId = null;
            this.ModName = string.Empty;
            this.ModDescription = string.Empty;
            this.ModAssembly = string.Empty;
            this.ModType = string.Empty;
            this.Target = null;
            this._moduleInstanceCollection = null;
            this._moduleParameterCollection = null;
            this._moduleParameterCollection2 = null;
            this._optionCollection = null;
        }

        private void Fill(System.Data.DataRow dr)
        {
            this.Clean();
            if ((dr["MOD_ID"] != System.DBNull.Value))
            {
                this.ModId = ((System.Nullable<System.Guid>)(dr["MOD_ID"]));
            }
            if ((dr["TAR_ID"] != System.DBNull.Value))
            {
                this._tarId = ((System.Nullable<System.Guid>)(dr["TAR_ID"]));
            }
            if ((dr["MOD_NAME"] != System.DBNull.Value))
            {
                this.ModName = ((string)(dr["MOD_NAME"]));
            }
            if ((dr["MOD_DESCRIPTION"] != System.DBNull.Value))
            {
                this.ModDescription = ((string)(dr["MOD_DESCRIPTION"]));
            }
            if ((dr["MOD_ASSEMBLY"] != System.DBNull.Value))
            {
                this.ModAssembly = ((string)(dr["MOD_ASSEMBLY"]));
            }
            if ((dr["MOD_TYPE"] != System.DBNull.Value))
            {
                this.ModType = ((string)(dr["MOD_TYPE"]));
            }
        }

        public static ModuleCollection Select_MODULE_By_TAR_ID(System.Nullable<System.Guid> TAR_ID)
        {
            IrisGenerator.Dal.Module dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Module();
                System.Data.DataSet ds = dbo.Select_MODULE_By_TAR_ID(TAR_ID);
                ModuleCollection collection = new ModuleCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        Module obj = new Module();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static ModuleCollection GetAll()
        {
            IrisGenerator.Dal.Module dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Module();
                System.Data.DataSet ds = dbo.MODULE_Select_All();
                ModuleCollection collection = new ModuleCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        Module obj = new Module();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static Module Load(System.Nullable<System.Guid> MOD_ID)
        {
            IrisGenerator.Dal.Module dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Module();
                System.Data.DataSet ds = dbo.MODULE_Select_One(MOD_ID);
                Module obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new Module();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public virtual void Load()
        {
            IrisGenerator.Dal.Module dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Module();
                System.Data.DataSet ds = dbo.MODULE_Select_One(this.ModId);
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        this.Fill(ds.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override void Insert()
        {
            IrisGenerator.Dal.Module dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Module();
                dbo.MODULE_Insert(this._tarId, this.ModName, this.ModDescription, this.ModAssembly, this.ModType);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override bool Delete(bool fireRuleMessages)
        {
            RuleErrorCollection errors = null;
            if (this.ObjectRules.CanDelete(ref errors))
            {
                IrisGenerator.Dal.Module dbo = null;
                try
                {
                    dbo = new IrisGenerator.Dal.Module();
                    BaseEventArgs<Module> e = new BaseEventArgs<Module>(this, fireRuleMessages);
                    this.OnDeleting(e);
                    if (!e.Cancel)
                    {
                        dbo.MODULE_Delete(this.ModId);
                        return true;
                    }
                    else
                        return false;
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if ((dbo != null))
                    {
                        dbo.Dispose();
                    }
                }
            }
            else
            {
                //TODO: Traitement des erreurs...
                return false;
            }
        }

        public override void Update()
        {
            IrisGenerator.Dal.Module dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Module();
                dbo.MODULE_Update(this.ModId, this._tarId, this.ModName, this.ModDescription, this.ModAssembly, this.ModType);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }
    }
}
